import {Song} from "../model/song"

Array.prototype.shuffle = function() {
    var array = this;
    var m = array.length,
        t, i;
    while (m) {
        i = Math.floor(Math.random() * m--);
        t = array[m];
        array[m] = array[i];
        array[i] = t;
    }
    return array;
}

let originPlayList = [
    new Song('ロマンスの神様','広瀬香美',"ロマンスの神様","/ablum/1.jpg","/ablum/music.m4a","1"),
    new Song('二零三','毛不易',"小王","/ablum/1.jpg","/ablum/203.m4a","1"),
    new Song('不染','毛不易',"电视剧香蜜沉沉","/ablum/1.jpg","/ablum/buran.m4a","2"),
    new Song('借','毛不易',"平凡人生","/ablum/1.jpg","/ablum/jie.m4a","3"),
    new Song('恋爱日记','毛毛',"恋爱日记","/ablum/1.jpg","/ablum/lianairiji.m4a","4"),
    new Song('像我这样的人','毛不易',"平凡人生","/ablum/1.jpg","/ablum/xiangwozheyang.m4a","5"),
    new Song('小丑','毛不易',"小丑","/ablum/1.jpg","/ablum/xiaochou.m4a","6"),
    new Song('一荤一素','毛不易',"小王","/ablum/1.jpg","/ablum/yihunyishu.m4a","7"),
    new Song('勇气','毛不易',"小王","/ablum/1.jpg","/ablum/yongqi.m4a","8"),
];






const state = {
    playing : false, // 正在播放
    screenMode : 1, // 展示类型 0为mini 1为全屏
    fullscreenMode: 0, // 0唱片循环 1歌词滚动
    MiniMode: 0, // 0 小布丁 1 播放列表
    originPlayList: originPlayList, // 原歌曲队列
    currentPlayIndex: 0, // 当前播放的歌曲索引
    currentPlayList: [...originPlayList], // 当前播放的歌曲队列
    playMode:0, // 播放模式 0 顺序播放 1 随机播放 2 单曲循环
};
const mutations = {
    setScreenMode(state, type){
        state.screenMode = type
    },
    playOrPause(state,type){
        state.playing = type
    },
    changePlayMode(state,type){
        state.playMode = type;
        switch(type){
            case 0:
                state.currentPlayList = [...state.originPlayList];
                break;
            case 1:
                state.currentPlayList = [...state.originPlayList].shuffle();
                break;
            case 2:
                state.currentPlayList = [state.currentPlayList[state.currentPlayIndex]];
                break;
        }
    },
    next(state){
        state.currentPlayIndex += 1
        if(state.currentPlayList.length - 1 < state.currentPlayIndex) {
            state.currentPlayIndex = 0
        }
        state.playing = true
    },
    pre(state){
        state.currentPlayIndex -= 1
        if(state.currentPlayIndex < 0 ){
            state.currentPlayIndex = state.currentPlayList.length -1
        }
        state.playing = true
    },
    changeFullscreenMode(state, type){
        state.fullscreenMode = type
    }
};
const actions = {
    // 如果需要异步请求的话写在这里
    setScreenMode({ commit }, type){
        commit('setScreenMode',type)
    }
};
const getters = {
    currentSong(state){
        if(state.currentPlayIndex == -1) {
            return {}
        }else{
            return state.currentPlayList[state.currentPlayIndex]
        }
    }
};
   
export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}